package db

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"

	"com.wen/model"
)

//获取首页的文章列表

//插入文章添加
func InsertArticle(article *model.ArticleDetail) (articleId int64, err error) {
	//连接数据库
	if article == nil {
		err = fmt.Errorf("invalid article parameter")
		return
	}
	//获取插入的数据
	sqlStr := `insert into article(content, summary, title, username, 
						category_id, view_count, comment_count) values(?,?,?,?,?,?,?)`
	result, err := DB.Exec(sqlStr,
		article.Content,
		article.ArticleInfo.Summary,
		article.ArticleInfo.Title,
		article.ArticleInfo.Username,
		article.ArticleInfo.CategoryId,
		article.ArticleInfo.ViewCount,
		article.ArticleInfo.CommentCount)
	if err != nil {
		return
	}
	articleId, err = result.LastInsertId() //插入 成功
	return
}

//获取文章列表list
func GetArticleList(pageNum, pageSize int) (articleList []*model.ArticleInfo, err error) {
	if pageNum < 0 || pageSize < 0 {
		err = fmt.Errorf("invalid parameter 输入的pagenubmer不对, page_num:%d, page_size:%d", pageNum, pageSize)
		return
	}
	//查询数据列表数据
	sqlStr := `select id,summary,title,view_count,create_time,comment_count,username,category_id from article  where status =1 order by create_time desc limit ?,?`
	err = DB.Select(&articleList, sqlStr, pageNum, pageSize)
	return
}

func GetArticleListByCategoryId(categoryId, pageNum, pageSize int) (articleList []*model.ArticleInfo, err error) {
	if pageNum < 0 || pageSize < 0 {
		err = fmt.Errorf("invalid parameter, page_num:%d, page_size:%d", pageNum, pageSize)
		return
	}

	sqlstr := `select 
						id, summary, title, view_count,
						 create_time, comment_count, username, category_id
					from 
						article 
					where 
						status = 1
						and
						category_id = ?
					order by create_time desc
					limit ?, ?`

	err = DB.Get(&articleList, sqlstr, categoryId, pageNum, pageSize)
	return
}

func GetArticleDetail(articleId int64) (articleDetail *model.ArticleDetail, err error) {
	if articleId < 0 {
		err = fmt.Errorf("invalid parameter,article_id:%d", articleId)
		return
	}
	articleDetail = &model.ArticleDetail{}
	sqlStr := `select id, summary, title, view_count, content,create_time, comment_count, username, category_id
						from 
							article 
						where 
							id = ?
						and
							status = 1`
	err = DB.Get(articleDetail, sqlStr, articleId)
	return
}

func GetCategoryById(categoryIds int64) (categoryList *model.Category, err error) {
	categoryList = &model.Category{}
	sqlstr := "select id, category_name, category_no from category where id=?"
	err = DB.Get(categoryList, sqlstr, categoryIds)
	return
}

func GetRelativeArticle(articleId int64) (articleList []*model.RelativeArticle, err error) {

	var categoryId int64
	sqlstr := "select category_id from article where id=?"
	err = DB.Get(&categoryId, sqlstr, articleId)
	if err != nil {
		return
	}

	sqlstr = "select id, title from article where category_id=? and id !=?  limit 10"
	err = DB.Select(&articleList, sqlstr, categoryId, articleId)
	return
}

func GetPrevArticleById(articleId int64) (info *model.RelativeArticle, err error) {

	info = &model.RelativeArticle{
		ArticleId: -1,
	}
	sqlstr := "select id, title from article where id < ? order by id desc limit 1"
	err = DB.Get(info, sqlstr, articleId)
	if err != nil {
		return
	}

	return
}

func GetNextArticleById(articleId int64) (info *model.RelativeArticle, err error) {

	info = &model.RelativeArticle{
		ArticleId: -1,
	}
	sqlstr := "select id, title from article where id > ? order by id asc limit 1"
	err = DB.Get(info, sqlstr, articleId)
	if err != nil {
		return
	}

	return
}
func IsArticleExist(articleId int64) (exists bool, err error) {

	var id int64
	sqlstr := "select id from article where id=?"
	err = DB.Get(&id, sqlstr, articleId)
	if err == sql.ErrNoRows {
		exists = false
		return
	}

	if err != nil {
		return
	}

	exists = true
	return
}
